/*
 * state.c
 *
 *  Created on: 2009-5-24
 *      Author: hwg
 */

#include "../header/common.h"
Status initlist_state(statelink *L , char *string)
{
	*L = malloc(sizeof(statenode));
	(*L)->next = NULL;
	buff bu;
	initbuff(&bu , string);
	int i = ok;
	while(i != error)
	{
		 statelink new_node = (statelink)malloc(sizeof(statenode));
		 elmAction knode;
		 int statenum;
		 i = getactionnode(&bu , &statenum ,&knode);
		 if(i == error)
			 return ok;
		 if(isexsits_st(*L , statenum , new_node) == ok)
		 {
			 addAction(&(new_node->data.unistate),knode);
		 }
		 else
		 {
			 InitAction(&(new_node->data.unistate) , statenum);
			 addAction(&(new_node->data.unistate),knode);
			 Insert_St(*L ,*new_node);
		 }

	}
	return ok;
}
Status getAction_st(statelink L , int statenum , char ident , elmAction *elAction)
{
	statenode new_node;
	ElemType 	e;
	if(isexsits_st( L , statenum , &new_node) == ok)
	{
		if(searchAction_stl(new_node.data.unistate,ident ,&e) == ok)
		{
			(*elAction) = e.uniaction;
			//printf("\n\t%s\t%s\t%d\t%d\n",(*elAction).action,(*elAction).identyfy,(*elAction).go,(*elAction).number);
			return ok;
		}
		else
			return error;
	}
	else
		return error;
}
Status isexsits_st(statelink L , int num ,statenode *snode)
{
	statelink p;
	if(L->next != NULL)
		p =L->next;
	else
		return error;
	while(p != NULL)
	{
		if(p->data.unistate.statenum == num)
		{
			*snode = *p;
			return ok;
		}
		else
		{
			p = p->next;
		}
	}
	return error;
}

Status Insert_St(statelink L, statenode e)
{
	statelink p=L;
	statelink new_node = malloc(sizeof(statenode));
	*new_node = e;
	new_node->next = p->next;
	p->next = new_node;
	return ok;
}

Status InitList_st(statelink *L)
{
	*L = malloc(sizeof(LNode));
	(*L)->next = NULL;
	return ok;
}
void printf_st(statelink L)
{
	statelink p = L;
	p = p->next;
	while(p != NULL)
	{
		printf("\nState=%d",p->data.unistate.statenum);
		printf_Action(p->data.unistate.table);
		p = p->next;
	}

}
